package mainProcess;

import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
import java.util.Vector;

import com.lo23.data.objinterface.*;
import com.lo23.data.objgroup.*;
import com.lo23.data.objpicture.*;

public class TraitementData {
	
	DataInterface dataInterface;
	
	public TraitementData()
	{	
		
	}
	
	public void setDataInterface(DataInterface dataInterface)
	{
		this.dataInterface = dataInterface;
		// user = dataInterface.getLocalUser("test");
	}
	
	public Friend getFriend(String senderUID)
	{
		List<Friend> friendList  = new ArrayList<Friend>();
		Friend friend = null ;
		
		friendList = dataInterface.getLocalUser(null).getListFriends();
		
		
		Iterator<Friend> it = friendList.iterator();

		//r�cup�ration de l'it�rateur

		while( it.hasNext()) {

		// it�ration de la liste

		      Friend f = (Friend) it.next();

		      if (f.getuID() == senderUID)
		    	  friend = f;

		}
		
		return friend;
		
	}
	
	public Picture getPicture(String pID)
	{
		List<Picture> pictureList  = new ArrayList<Picture>();
		pictureList = dataInterface.getLocalUser(null).getListPictures();
		Picture pic = null;
		
		Iterator<Picture> it = pictureList.iterator();


		while( it.hasNext()) {

			Picture p = (Picture) it.next();

		      if (p.getpID() == pID)
		    	  pic = p;

		}
		return pic;
	}
	
	public AccRight unionAccesRight(AccRight a1,AccRight a2)
	{
		AccRight ar;
		
		if ((a1 == null) && (a2 == null))
			ar = new AccRight(false,false,false,false);
		else if (a1 == null)
			ar = new AccRight(a2);
		else if (a2 == null)
			ar = new AccRight(a1);

		else {
			boolean pictureRight = a1.pictureRight || a2.pictureRight;
			boolean commentRight = a1.commentRight || a2.commentRight;
			boolean viewCommentRight= a1.viewCommentRight || a2.viewCommentRight;
			//	boolean profileRight= a1.getProfileRight() || a2.getProfileRight();
			boolean markRight= a1.markRight || a2.markRight;
		
		
			ar = new AccRight(pictureRight,commentRight,viewCommentRight,markRight);
		}
		return ar;

	}
	
	public AccRight getBestRightForPicture(String senderUID,String pID)
	{	
		// va renvoeyr surtout les variables booleennes, car l'union des droites enleve le reste

		Picture pic = null;
		Friend friend = null;
		Album album = null;
		Group group = null;
		AccRight accessRightPicture = null;
		AccRight accessRightGroup = null;
		AccRight accessRightTemp = null;
		AccRight accessRightAlbum = null;
		AccRight accessRightAlbumGroup = null;
		// TODO mettre tout a false :
		//AccRight picRights = new AccRight(true, true,true,true);
		 AccRight picRights = new AccRight(false,false,false,false);
		
		pic = getPicture(pID);
		friend = getFriend(senderUID);
		
		if (pic!= null )
		{
	
			List <Group> groupes = dataInterface.getLocalUser(null).getListGroups();
			List <Album> albums = dataInterface.getLocalUser(null).getListAlbums();
			
			
			
	
			for (Album a : albums) {
				if (a != null && a.getListOfPictures().contains(pic))
					album = a;
			}
			
			
			
			for (Group g : groupes) {
				if (g != null && dataInterface.getLocalUser(null).isFriendinGroup(senderUID, g.getName()))
				{
					accessRightGroup = new AccRight(dataInterface.getLocalUser(null).getGroupPictureRights(g,pic));
					if (album !=null)
						accessRightAlbumGroup = new AccRight(dataInterface.getLocalUser(null).getGroupAlbumRights(g, album));
					
					
					accessRightTemp = unionAccesRight(accessRightAlbumGroup,accessRightGroup);
					picRights = unionAccesRight(picRights, accessRightTemp);
					
				}
			}
			
			
			// on recupere le droit relatif a,.. l'album
			if (album!=null)
				accessRightAlbum = new AccRight(dataInterface.getLocalUser(null).getUserAlbumRights(friend,album));
			
			
			// on recupere le droit relatif a la photo
			
			
			accessRightPicture = new AccRight(dataInterface.getLocalUser(null).getUserPictureRights(friend, pic));
				
			
			// on fait l'union des droits du group et de l'dataInterface.getLocalUser(null)
			
			picRights = unionAccesRight(picRights,accessRightAlbum);
			picRights = unionAccesRight(picRights,accessRightPicture);

			
			
			
		}
		
		return picRights;
	}
	
	public boolean CanSeeImage(String senderUID,String pID)
	{
		boolean test = false;
		AccRight ac;
		ac = getBestRightForPicture(senderUID,pID);
		
		if (ac!= null )
			test = ac.pictureRight;
		
		return test;
		
	}
	
	public boolean CanCommentImage(String senderUID,String pID)
	{
		boolean test = false;
		AccRight ac;
		ac = getBestRightForPicture(senderUID,pID);
		
		if (ac!= null )
			test = ac.commentRight;
		
		return test;
		
	}
	
	public boolean canSeeAlbum(String aID, String senderUI)
	{
		Friend friend = null;
		AccRight accessRightAlbum = null;
		List <Album> albums = dataInterface.getLocalUser(null).getListAlbums();
		friend = getFriend(senderUI);
		if (friend == null)
			return false; // erreur
		
		for (Album a : albums) {
			if (a.getaID() == aID )
			{
				accessRightAlbum = new AccRight(dataInterface.getLocalUser(null).getUserAlbumRights(friend,a));
				if (accessRightAlbum.pictureRight)
					return true;
			}
		}
		return false;
	}
	
	
	
	public boolean CanViewImageComment(String senderUID,String pID)
	{
		boolean test = false;
		AccRight ac;
		ac = getBestRightForPicture(senderUID,pID);
		
		if (ac!= null )
			test = ac.viewCommentRight;
		
		return test;
		
	}

	
	public boolean CanMarkImage(String senderUID,String pID)
	{
		boolean test = false;
		AccRight ac;
		ac = getBestRightForPicture(senderUID,pID);
		
		if (ac!= null )
			test = ac.markRight;
		
		return test;
		
	}
	
	public boolean canSeeProfile( String senderUID) 
	{
		return (getFriend(senderUID) != null);
	}
	
	public List<Picture> getVisibleImages(String senderUID)
	{
		List<Picture> pictureList = new ArrayList<Picture>();
		List<Picture> resultList= new ArrayList<Picture>();
		pictureList = dataInterface.getLocalUser(null).getListPictures();
		
		
		
		Iterator<Picture> it = pictureList.iterator();

		//r�cup�ration de l'it�rateur

		while( it.hasNext()) {

		// it�ration de la liste

		      Picture p = (Picture) it.next();
		      if ( CanSeeImage( senderUID,p.getpID()))
		    	  resultList.add(p);
		}
		
		return resultList;
	}

}


class AccRight
{
	public 
	boolean pictureRight ;
	boolean commentRight ;
	boolean viewCommentRight;
	boolean markRight ;
	AccRight(boolean pr,boolean cr,boolean wcr,boolean mr) 
	{
		pictureRight = pr ;
		commentRight = cr;
		viewCommentRight =wcr;
		markRight = mr;
		
	}
	
	AccRight(AccessRights ar) 
	{
		if (ar != null)
		{
			pictureRight = ar.getPictureRight() ;
			commentRight = ar.getCommentRight();
			viewCommentRight = ar.getViewCommentRight();
			markRight = ar.getMarkRight();
		}
		else
		{
			pictureRight = true;
			commentRight = true;
			viewCommentRight = true;
			markRight = true;
		}
		
	}
	
	AccRight(AccRight ar) 
	{
		if (ar != null)
		{
			pictureRight = ar.pictureRight ;
			commentRight = ar.commentRight;
			viewCommentRight = ar.viewCommentRight;
			markRight = ar.markRight;
		}
		else
		{
			pictureRight = true ;
			commentRight = true ;
			viewCommentRight = true ;
			markRight = true ;
		}
		
	}
}
